package com.timoon.component.panel.jqgrid.action;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.form.Form;

import com.googlecode.wicket.jquery.ui.form.button.AjaxButton;
import com.timoon.component.panel.jqgrid.JqGridPanel;
import com.timoon.component.panel.jqgrid.panel.CreatorPanel;

/**
 * jqgrid附属的【新增按钮】
 * @author Administrator
 *
 */
public class AddButton extends AjaxButton {
	private static final long serialVersionUID = -5671091380892230388L;
	
	public static String WIN_TITLE_OF_CONFIG_MAP_KEY="5671091380892230388_win_title";

	private ModalWindow win;
	private JqGridPanel mainPanel;
	
	public AddButton(String id,JqGridPanel mainPanel) {
		super(id);
		// TODO Auto-generated constructor stub
		if(!mainPanel.isButtonVisible(JqGridPanel.ADD_BUTTON_ENABEL)){
			this.setVisible(false);
		}
		
		this.win=mainPanel.getWin();
		this.mainPanel=mainPanel;
	}
	@Override
	protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
		// TODO Auto-generated method stub
		super.onSubmit(target, form);
		win.setMinimalWidth(400);
		win.setTitle(mainPanel.getConfigByKey(WIN_TITLE_OF_CONFIG_MAP_KEY));
		win.setContent(this.getCreatorPanel(win.getContentId(), mainPanel));
		win.show(target);
	}
	/**
	 * 从mainPanel获取编辑面板
	 * @param id
	 * @param mainPanel
	 * @return
	 */
	private CreatorPanel getCreatorPanel(String id,JqGridPanel mainPanel){
		return mainPanel.getCreatorPanel(id, mainPanel,null);
	}
}
